Page({
  data: {
    info: [
      { title: "认证中心", url: "/pages/creator/profile", icon: "user-o" },
      // { title: "我的预约", url: "./reservation/list", icon: "calendar-o" },
    ],
    info_extra: [
      // { title: "联系客服", icon: "contact" },
      // { title: "设置", icon: "setting-o" },
    ],
    admin_menu: { title: "后台管理", url: "/pages/admin/index", icon: "manager-o" },
    userInfo: null,
  },
  onLoad(options) {
  },
  onShow() {
    this.setUserInfo();
  },
  async onLogin() {
    const userInfo = await new Promise(resolve => {
      wx.getUserProfile({
        desc: '获取您的账号和头像用于登录',
        success: ({ userInfo }) => resolve(userInfo),
        fail: () => resolve(null)
      });
    });
    if (!!userInfo) {
      this.setUserInfo(userInfo);
    }
  },
  async setUserInfo(userInfo) {
    const app = getApp();
    if (!userInfo) {
      this.setData({ userInfo: app.globalData.userInfo });
    } else {
      const res = await new Promise(resolve => {
        wx.cloud.callFunction({
          name: 'login',
          data: { avatar: userInfo.avatarUrl, username: userInfo.nickName },
          success: (r) => resolve(r.result),
        })
      });
      app.globalData.userInfo = res.userInfo;
      this.setData({ userInfo: res.userInfo });
    }
    userInfo = this.data.userInfo;
    const info = [
      { title: "认证中心", url: "/pages/creator/profile", icon: "user-o" },
    ];
    const _addMenu = [
      { title: "我的钱包", url: "./balance/list", icon: "balance-pay" },
      { title: "我的订单", url: "./order/list", icon: "orders-o" },
      { title: "核销管理", url: "./coupon/index", icon: "todo-list-o" },
    ]
    if (userInfo.role_id == 3) {
      info.push(_addMenu[0], _addMenu[1])
    } else if (userInfo.role_id == 2) {
      info.push(_addMenu[2])
    } else if (userInfo.role_id == 1) {
      info.push(_addMenu[0], _addMenu[2])
    }
    this.setData({ info })
  },
})
